package com.lzj.server.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 核心配置类
 * 集中声明所有的 Exchanges, Queues, Bindings, 以及死信队列
 */
@Configuration
public class RabbitMQConfig {

    // ========== 交换机名称常量 ==========
    public static final String EXCHANGE_VIDEO = "exchange.topic.video";
    public static final String EXCHANGE_USER_ACTION = "exchange.topic.user.action";

    // ========== 队列名称常量 ==========
    public static final String QUEUE_VIDEO_EMBEDDING_GENERATION = "queue.video.embedding.generation";
    public static final String QUEUE_USER_ACTION_ANALYTICS = "queue.user.action.analytics";
    public static final String QUEUE_USER_PROFILE_UPDATE = "queue.user.profile.update";

    // ========== 死信交换机和队列常量 ==========
    public static final String DLX_EXCHANGE = "exchange.dlx.general";
    public static final String DLX_QUEUE = "queue.dlx.general";

    // 1. 声明死信交换机 (DLX) 和死信队列 (DLQ)
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DLX_EXCHANGE);
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DLX_QUEUE);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("#");
    }

    // 2. 声明业务交换机
    @Bean
    public TopicExchange videoExchange() {
        return new TopicExchange(EXCHANGE_VIDEO);
    }

    @Bean
    public TopicExchange userActionExchange() {
        return new TopicExchange(EXCHANGE_USER_ACTION);
    }

    // 3. 声明业务队列，并为每个队列绑定死信交换机
    @Bean
    public Queue videoEmbeddingQueue() {
        // 当这个队列里的消息处理失败后，会被发送到 DLX_EXCHANGE
        return QueueBuilder.durable(QUEUE_VIDEO_EMBEDDING_GENERATION)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .build();
    }

    @Bean
    public Queue userActionAnalyticsQueue() {
        return QueueBuilder.durable(QUEUE_USER_ACTION_ANALYTICS)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .build();
    }

    @Bean
    public Queue userProfileUpdateQueue() {
        return QueueBuilder.durable(QUEUE_USER_PROFILE_UPDATE)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .build();
    }

    // 4. 声明绑定关系
    @Bean
    public Binding videoEmbeddingBinding() {
        // 监听 "video.uploaded" 事件
        return BindingBuilder.bind(videoEmbeddingQueue())
                .to(videoExchange())
                .with("video.uploaded");
    }

    @Bean
    public Binding analyticsBinding() {
        // 分析服务订阅所有用户行为
        return BindingBuilder.bind(userActionAnalyticsQueue())
                .to(userActionExchange())
                .with("user.action.#");
    }

    @Bean
    public Binding userProfileBinding() {
        // 用户画像服务只订阅点赞和收藏行为
        return BindingBuilder.bind(userProfileUpdateQueue())
                .to(userActionExchange())
                .with("user.action.like.*");
    }

    @Bean
    public Binding userProfileFavoriteBinding() {
        return BindingBuilder.bind(userProfileUpdateQueue())
                .to(userActionExchange())
                .with("user.action.favorite.*");
    }
}